package java0108;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Java05 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        // Java基础API体系：集合、异常、IO
        // Java高级API体系：网络、反射、线程

        // 反射理解：一种看透和调用Java的能力!
        // 类            java.lang.Class
        // 成员变量       java.lang.reflect.Filed
        // 成员方法       java.lang.reflect.Method
        // 构造函数       java.lang.reflect.Contructor

        // 根据报名.类名、获取一个类对象
        Class c = Class.forName("java0108.Person");
        // 成员变量
        Field[] declaredFields = c.getDeclaredFields();
        for (Field f : declaredFields) {
            System.out.println(f.getName());
        }
        // 成员方法
        Method[] declaredMethods = c.getDeclaredMethods();
        for (Method m : declaredMethods) {
            System.out.println(m.getName());
        }
        // 构造函数
        Constructor[] declaredConstructors = c.getDeclaredConstructors();
        for (Constructor m : declaredConstructors) {
            System.out.println(m.getName());
        }
        // 获取父类
        Class superclass = c.getSuperclass();
        System.out.println(superclass.getName());
        // 获取接口
        Class[] interfaces = c.getInterfaces();
        for (Class i : interfaces) {
            System.out.println(i.getName());
        }

        // 创建对象
        Person person = new Person();
        person.setName("张三");
        System.out.println(person);

        // 使用反射API创建对象
        Person person2 = (Person)c.newInstance();
        person2.setName("李四");
        System.out.println(person2);

    }
}

class F {

}

interface X {

}

interface Y {

}

class Person extends F implements X, Y{
    String name;
    int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

}
